#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;

#endregion

namespace GrapplingHookGame
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        #region Initialization


        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base(Resources.MainMenu)
        {
            BackgroundFade = 0;

            // Create our menu entries.
            MenuEntry localPlayerMenuEntry = new MenuEntry(Resources.LocalPlayer);
            MenuEntry liveMenuEntry = new MenuEntry(Resources.PlayerMatch);
            MenuEntry systemLinkMenuEntry = new MenuEntry(Resources.SystemLink);
            MenuEntry exitMenuEntry = new MenuEntry(Resources.Exit);
            MenuEntry helpMenuEntry = new MenuEntry(Resources.Help);

            // Hook up menu event handlers.
            localPlayerMenuEntry.Selected += LocalPlayerMenuEntrySelected;
            liveMenuEntry.Selected += LiveMenuEntrySelected;
            systemLinkMenuEntry.Selected += SystemLinkMenuEntrySelected;
            helpMenuEntry.Selected += HelpMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(localPlayerMenuEntry);
            MenuEntries.Add(liveMenuEntry);
            MenuEntries.Add(systemLinkMenuEntry);
            MenuEntries.Add(helpMenuEntry);
            MenuEntries.Add(exitMenuEntry);

            //accept invites
            NetworkSession.InviteAccepted += OnInviteAccepted;




        }

        public override void LoadContent()
        {
            base.LoadContent();
            Position = new Vector2(220, 350);


            // Search through Game.Components to find the SoundComponent.
            foreach (IGameComponent component in ScreenManager.Game.Components)
            {
                SoundComponent soundComponent = component as SoundComponent;
                if (soundComponent != null)
                {
                    //play menu music
                    soundComponent.PlayBackgroundMusic("MenuMusic");
                    break;
                }
            }
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Single Player menu entry is selected.
        /// </summary>
        void LocalPlayerMenuEntrySelected(object sender, EventArgs e)
        {

            // First, we need to make sure a suitable gamer profile is signed in, even for the local game, because we are uring network components.
            ProfileSignInScreen profileSignIn = new ProfileSignInScreen(NetworkSessionType.Local);

            // Hook up an event so once the ProfileSignInScreen is happy,
            // it will activate the lobby screen.
            profileSignIn.ProfileSignedIn += delegate
            {
                //create network session
                NetworkSession networkSession = NetworkSession.Create(NetworkSessionType.Local, NetworkSessionComponent.MaxLocalGamers, NetworkSessionComponent.MaxLocalGamers);
                NetworkSessionComponent.Create(ScreenManager, networkSession);

                // Go to the lobby screen.
                ScreenManager.AddScreen(new LobbyScreen(networkSession));               
            };

            // Activate the ProfileSignInScreen.
            ScreenManager.AddScreen(profileSignIn);
        }

        /// <summary>
        /// Event handler for when the Help menu entry is selected.
        /// </summary>
        void HelpMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen( new HelpScreen());
        }

        /// <summary>
        /// Event handler for when the Live menu entry is selected.
        /// </summary>
        void LiveMenuEntrySelected(object sender, EventArgs e)
        {
            if (Guide.IsTrialMode)
            {
                ScreenManager.AddScreen(new TrialScreen());
            }
            else
            {
                CreateOrFindSession(NetworkSessionType.PlayerMatch);
            }
        }


        /// <summary>
        /// Event handler for when the System Link menu entry is selected.
        /// </summary>
        void SystemLinkMenuEntrySelected(object sender, EventArgs e)
        {
            CreateOrFindSession(NetworkSessionType.SystemLink);
        }


        /// <summary>
        /// Helper method shared by the Live and System Link menu event handlers.
        /// </summary>
        void CreateOrFindSession(NetworkSessionType sessionType)
        {
            // First, we need to make sure a suitable gamer profile is signed in.
            ProfileSignInScreen profileSignIn = new ProfileSignInScreen(sessionType);

            // Hook up an event so once the ProfileSignInScreen is happy,
            // it will activate the CreateOrFindSessionScreen.
            profileSignIn.ProfileSignedIn += delegate
            {
                ScreenManager.AddScreen(new CreateOrFindSessionScreen(sessionType));
            };

            // Activate the ProfileSignInScreen.
            ScreenManager.AddScreen(profileSignIn);
        }


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            MessageBoxScreen confirmExitMessageBox =
                                    new MessageBoxScreen(Resources.ConfirmExitSample);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        void OnInviteAccepted(object sender, InviteAcceptedEventArgs args)
        {
            // Quit the current session
            /*
            if (networkSession != null)
            {
                networkSession.Dispose();
                networkSession = null;
            }
            */
            GameSession.EndSession();

            // Join the new session
            NetworkSession networkSession = NetworkSession.JoinInvited(NetworkSessionComponent.MaxLocalGamers);

            // Create a component that will manage the session we just joined.
            NetworkSessionComponent.Create(ScreenManager, networkSession);

            // Go to the lobby screen.
            ScreenManager.AddScreen(new LobbyScreen(networkSession));
        }

        #endregion
    }
}
